package com.feng.vhr.response;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.feng.vhr.pojo.Hr;
import com.feng.vhr.pojo.Role;
import com.feng.vhr.utils.CustomAuthorityDeserializer;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class RHr extends Hr implements UserDetails {
    private List<Role> roles;

    @Override
    @JsonDeserialize(using = CustomAuthorityDeserializer.class)
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> premiss = new ArrayList<>();
        premiss=roles.stream().map(item->{
            String name = item.getName();
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(name);
            return simpleGrantedAuthority;
        }).collect(Collectors.toList());
        System.out.println(premiss);
        return premiss;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return super.getEnabled();
    }
}
